package com.shijunhao.graduation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shijunhao.graduation.annotations.OperationLog;
import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.domain.bean.WangEditorResponse;
import com.shijunhao.graduation.domain.dto.BusinessForumDto;
import com.shijunhao.graduation.domain.dto.BusinessForumQueryDto;
import com.shijunhao.graduation.domain.dto.BusinessNoteDto;
import com.shijunhao.graduation.domain.entity.BusinessForum;
import com.shijunhao.graduation.domain.entity.BusinessNote;
import com.shijunhao.graduation.domain.entity.SystemFileInfo;
import com.shijunhao.graduation.domain.vo.*;
import com.shijunhao.graduation.enums.TableName;
import com.shijunhao.graduation.properties.OperationTarget;
import com.shijunhao.graduation.properties.OperationType;
import com.shijunhao.graduation.service.business.IBusinessForumService;
import com.shijunhao.graduation.service.scheduled.LooksScheduledService;
import com.shijunhao.graduation.service.system.ISystemFileInfoService;
import com.shijunhao.graduation.utils.PageUtils;
import com.shijunhao.graduation.utils.WrapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/business-forum")
@Api(tags = "论坛管理")
public class BusinessForumController {

    @Resource
    private IBusinessForumService businessForumService;

    @Resource
    private ISystemFileInfoService fileInfoService;

    @Resource
    private LooksScheduledService looksScheduledService;

    @GetMapping("/list")
    @ApiOperation("列表")
    public ResultCode<IPage<BusinessForumListVo>> page(@ModelAttribute BusinessForumQueryDto query){
        Page<BusinessForum> page = businessForumService.page(PageUtils.getPage(), WrapperUtils.query(query));
        return ResultCode.success(page.convert(businessForumService::toListVo));
    }

    @PostMapping("insert")
    @ApiOperation("新增")
    @OperationLog(target = OperationTarget.FORUM,type = OperationType.INSERT,description = "新增论坛")
    public ResultCode<Long> insert(@RequestBody BusinessForumDto dto){
        Long insert = businessForumService.insert(dto);
        if(insert != null)
            return ResultCode.success("插入成功",insert);
        else
            return ResultCode.error("插入失败");
    }

    @PostMapping("delete/{id}")
    @ApiOperation("删除")
    @OperationLog(target = OperationTarget.FORUM,type = OperationType.DELETE,description = "删除论坛")
    public ResultCode<Boolean> delete(@PathVariable Long id){
        boolean delete = businessForumService.delete(id);
        if(delete)
            return ResultCode.success("删除成功");
        else
            return ResultCode.error("删除失败");
    }

    @PostMapping("/upload")
    @ApiOperation("论坛编辑图片上传")
    public WangEditorResponse fileUpload(@RequestParam("pic") MultipartFile file){
        SystemFileInfo fileInfo = fileInfoService.upload(file);
        if(fileInfo == null){
            return WangEditorResponse.error("上传失败");
        }
        return WangEditorResponse.success(fileInfo);
    }


    @GetMapping("info/{id}")
    @ApiOperation("详情")
    public ResultCode<BusinessForumVo> info(@PathVariable Long id){
        BusinessForum info = businessForumService.getById(id);
        if(info != null){
            looksScheduledService.userLook(TableName.BUSINESS_FORUM.getLabel(),id);
            BusinessForumVo mapping = businessForumService.mapping(info);
            return ResultCode.success(mapping);
        }else{
            return ResultCode.error("没有找到这条信息。");
        }
    }

    @GetMapping("forum/info/{id}")
    @ApiOperation("简要详情")
    public ResultCode<BusinessForum> forumInfo(@PathVariable Long id){
        BusinessForum info = businessForumService.getById(id);
        if(info != null){
            return ResultCode.success(info);
        }else{
            return ResultCode.error("没有找到这条信息。");
        }
    }

    @PostMapping("update")
    @ApiOperation("更新")
    @OperationLog(target = OperationTarget.FORUM,type = OperationType.UPDATE,description = "更新论坛")
    public ResultCode<Boolean> update(@RequestBody BusinessForumDto dto) {
        boolean update = businessForumService.update(dto);
        if (update)
            return ResultCode.success("更新成功");
        else
            return ResultCode.error("更新失败");
    }

    @GetMapping("/list/mine")
    @ApiOperation("我的论坛列表")
    public ResultCode<IPage<BusinessForumMineVo>> pageMine(){
        return ResultCode.success(businessForumService.pageMine(PageUtils.getPage()));
    }

}
